<template>
  <div>
      <el-form :model="searchForm" inline label-width="80px" v-if="searchForm.type === 1 || searchForm.type === 2">
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchForm.timeRange"
            type="datetimerange"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="驾驶方式">
          <el-select v-model="searchForm.isDriver" clearable placeholder="选择驾驶方式">
            <el-option label="指派司机" :value="1"/>
            <el-option label="自己驾驶" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="服务状态">
          <el-select v-model="searchForm.applyStatus" clearable placeholder="选择服务状态">
            <el-option label="员工申请" :value="1"/>
            <el-option label="部门同意" :value="2"/>
            <el-option label="部门拒绝" :value="3"/>
            <el-option label="同意通车" :value="4"/>
            <el-option label="拒绝用车" :value="5"/>
            <el-option label="用车结束" :value="6"/>
            <el-option label="异常订单" :value="7"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataList">搜索</el-button>
          <el-button @click="clearForm">清空</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="dataList" border>
        <el-table-column prop="applyName" label="申请人" align="center"/>
        <el-table-column prop="applyStartTime" label="开始时间" align="center"/>
        <el-table-column prop="applyEndTime" label="结束时间" align="center"/>
        <el-table-column prop="applyRemake" show-overflow-tooltip label="用车原因" align="center"/>
        <el-table-column prop="carNo" label="申请车辆" align="center"/>
        <el-table-column prop="adminCarNo" label="实际车辆" align="center"/>
        <el-table-column prop="driverName" label="司机姓名" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isDriver === 1">{{scope.row.driverName}}</span>
            <span v-else>申请人自驾</span>
          </template>
        </el-table-column>
        <el-table-column prop="returnTime" label="还车时间" align="center"/>
        <el-table-column prop="status" label="服务状态" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" type="primary" v-if="scope.row.applyStatus === 1">员工申请</el-tag>
            <el-tag size="mini" type="success" v-else-if="scope.row.applyStatus === 2">部门同意</el-tag>
            <el-tag size="mini" type="danger" v-else-if="scope.row.applyStatus === 3">部门拒绝</el-tag>
            <el-tag size="mini" type="success" v-else-if="scope.row.applyStatus === 4">同意用车</el-tag>
            <el-tag size="mini" type="danger" v-else-if="scope.row.applyStatus === 5">拒绝用车</el-tag>
            <el-tag size="mini" type="warning" v-else-if="scope.row.applyStatus === 6">用车结束</el-tag>
            <el-tag size="mini" type="danger" v-else>服务异常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160px">
          <template slot-scope="scope">
            <el-button size="mini" type="info" @click="openDialog(scope.row.id,'detail')">详情</el-button>
            <el-button size="mini" v-if="(scope.row.applyStatus === 1 && loginInfo.roleId === 3) || (scope.row.applyStatus === 2 && loginInfo.roleId === 2) && searchForm.type === 1" @click="apply(scope.row.id)">修改</el-button>
            <el-button size="mini" v-if="loginInfo.roleId === 1 && (scope.row.applyStatus === 6 || scope.row.applyStatus === 7)" @click="deleteInfo(scope.row.id)">删除</el-button>
            <el-button v-if="searchForm.type === 3" size="mini" type="primary" @click="openDialog(scope.row.id,'exam')">审核</el-button>
            <el-button v-if="searchForm.type === 1 && scope.row.applyStatus === 4" size="mini" type="primary" @click="returnCar(scope.row.id)">还车</el-button>
          </template>
        </el-table-column>
      </el-table>
    <el-pagination hide-on-single-page :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" @current-change="currentChange" @size-change="sizeChange" layout="total,sizes,prev,pager,next,jumper" :total="pageInfo.total"/>
    <apply-detail ref="applyDetail" @closeDialog="getDataList"/>
  </div>
</template>

<script>

/*
*type
* 1:个人订单
* 2:所有订单(分责任和管理员看到的所有订单是不同)
* 3:待审核
* 4:同意用车
*
* */
import ApplyDetail from '@/views/apply/ApplyDetail.vue'

const initData = {
  timeRange: [],
  isDriver: '',
  applyStatus: '',
  type: 1
}
export default {
  name: 'ApplyList',
  components: { ApplyDetail },
  data () {
    return {
      loginInfo: {},
      searchForm: {
        ...initData
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      dataList: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {
    getDataList () {
      const timeRange = this.searchForm.timeRange.join()
      this.$http.get('apply/page', { ...this.searchForm, timeRange, ...this.pageInfo }).then(data => {
        if (data) {
          this.dataList = [...data.rows]
          this.pageInfo.total = data.count
        }
      }).catch(() => {})
    },
    currentChange (page) {
      this.pageInfo.page = page
      this.getDataList()
    },
    sizeChange (limit) {
      this.pageInfo.limit = limit
      this.getDataList()
    },
    clearForm () {
      this.searchForm = {
        ...this.searchForm,
        timeRange: [],
        isDriver: '',
        applyStatus: ''
      }
      this.pageInfo = {
        page: 1,
        limit: 5,
        total: 0
      }
      this.getDataList()
    },
    deleteInfo (id) {
      this.$confirm(`确定删除编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的数据吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.delete(`apply/delete/${id}`).then(() => {
          this.$message.success('删除成功！')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    apply (id) {
      this.$router.push('apply?id=' + id)
    },
    openDialog (id, type) {
      this.$refs.applyDetail.init(id, type)
    },
    returnCar (id) {
      this.$confirm('确定还车吗?', '提示').then(() => {
        this.$http.post('apply/return/car', { id }).then(() => {
          this.$message.success('操作成功！')
          this.getDataList()
        }).catch(() => {

        })
      }).catch(() => {})
    },
    setType (route) {
      this.searchForm.type = route.meta.type
      this.clearForm()
      this.$http.get('userinfo/info').then(data => {
        this.loginInfo = { ...data }
      }).catch(() => {})
    }
  },
  watch: {
    $route (to) {
      this.setType(to)
    }
  },
  created () {
    this.setType(this.$route)
  }
}
</script>

<style scoped>

</style>
